﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.IO;
using System.Data.OleDb;


using Srvtools;
using System.Text;

using System.Reflection;
using System.Diagnostics;


public partial class EmployeesSalary_DownLoadSalaryTemplate : System.Web.UI.Page
{
    private Srvtools.WebDataSet WMaster;


    protected void Page_Load(object sender, EventArgs e)
    {
        ((HiddenField)EmpSalaryListUpDown.FindControl("HoverId")).Value = "ListDownLoad";
        if (!IsPostBack)
        {
            SelUserMultiple.Width = 400;
            SelUserMultiple.Height = 400;


            MasterPage_MasterPage master = (MasterPage_MasterPage)this.Page.Master;
            master.HelpHref = "";
            master.PageName = "薪资模版下载";
            master.PicUrl = "";
            master.IntiBar();


            InitializeComponent(false);
        }
    }

    private void InitializeComponent(bool action)
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(EmployeesSalary_DownLoadSalaryTemplate));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = action;
        this.WMaster.AlwaysClose = false;
        this.WMaster.PacketRecords = -1;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "SEmployees.ViewDownLoadSalaryList";
        this.WMaster.ServerModify = false;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();

    }


    protected void UpLoad_Click(object sender, EventArgs e)
    {
        if (SelUserMultiple.GetUserList() != null)
        {
            string a = SelUserMultiple.GetUserList();
        }

        string path = MapPath("~/UploadFile/Salary/Template/");
        string temppath = path + "temp.xls";
        string Ulist = SelUserMultiple.GetUserList();

        if (Ulist != "" && Ulist != "undefined")
        {

            //object[] ret = Srvtools.CliUtils.CallMethod("SEmployees", "DownLoadSalaryList", new object[] { Ulist });
            //DataTable dt = ((DataSet)ret[1]).Tables[0];

            //wds.DataSource = dt;
            //wds.DataMember = "ViewWds";

            //wep.DataSource[0].DataSource = wds;
            //wep.DataSource[0].Caption = "wds";
            InitializeComponent(true);
            wds.DataSource = WMaster;
            wds.SetWhere("Employees.EmployeeId in (" + Ulist + ")");

            wep.Output(1);


            /*
            ExcelHelper ex = new ExcelHelper(path + file, temppath);
            ex.CopyRows(1, 2, dt.Rows.Count);

            
            //ex.DataTableToExcel(dt, 3, 1);


            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ex.SetCells(1, i + 3, 1, "0");
                ex.SetCells(1, i + 3, 2, dt.Rows[i]["sid"].ToString());
                ex.SetCells(1, i + 3, 3, dt.Rows[i]["ename"].ToString());
                ex.SetCells(1, i + 3, 4, dt.Rows[i]["dname"].ToString());
                ex.SetCells(1, i + 3, 5, dt.Rows[i]["fname"].ToString());
                ex.SetCells(1, i + 3, 6, dt.Rows[i]["jbgz"].ToString());
                ex.SetCells(1, i + 3, 8, dt.Rows[i]["gwjt"].ToString());
                ex.SetCells(1, i + 3, 7, dt.Rows[i]["glbt"].ToString());
            }
            
            ex.DeleteRows(2, 1);
            ex.SaveAsFile(path + "temp.xls");
            
            //ExecleDs(path + "temp.xls", dt);*/
            FileInfo fn = new System.IO.FileInfo(temppath);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fn.Name);
            HttpContext.Current.Response.WriteFile(fn.FullName);
            HttpContext.Current.Response.End();

            Response.Redirect(Request.Url.ToString());
        }
    }
}
